from ninja import Router

from apps.schemas import R
from models.models import SearchRecord
from utils.auth import AppletAuthBearer
from utils.decorators import handle_exception

search_record_router = Router(tags=["搜索记录"], auth=AppletAuthBearer())


@search_record_router.get("/search_records", summary="获取搜索记录")
@handle_exception("获取搜索记录失败")
def get_search_records(request):
    records = SearchRecord.objects.filter(user_id=request.user.id).values_list("keyword", flat=True)
    return R.ok(data=list(records))


@search_record_router.get("/search_records/delete", summary="删除搜索记录")
@handle_exception("删除搜索记录失败")
def delete_search_records(request):
    SearchRecord.objects.filter(user=request.user).delete()
    return R.ok(msg="删除成功")

